//
//  ZHFindRentMsgDetailsViewController.m
//  sc-cyan-ios
//
//  Created by lj on 2018/1/5.
//  Copyright © 2018年 greenbuilding. All rights reserved.
//

#import "ZHFindRentMsgDetailsViewController.h"

#import "ZHFindRentMsgDetailsOneCell.h"
#import "ZHFindRentMsgDetailsTowCell.h"
#import "ZHFindRentMsgDetailsThreeCell.h"
#import "ZHFindRentMsgDetailsFooterView.h"

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

static NSString *const cellOne = @"ZHFindRentMsgDetailsOneCell";
static NSString *const cellTow = @"ZHFindRentMsgDetailsTowCell";


@interface ZHFindRentMsgDetailsViewController ()<UITableViewDataSource,UITableViewDelegate,BMKMapViewDelegate,BMKLocationServiceDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//左侧标题
@property (nonatomic,strong) NSArray *leftTitleList;

//地图
@property (nonatomic,strong) BMKMapView* mapView;
//定位服务
@property (nonatomic,strong) BMKLocationService *locService;
@end

@implementation ZHFindRentMsgDetailsViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    [self.mapView viewWillAppear];
    self.mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil; // 不用时，置nil
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpView];
}

- (NSArray *)leftTitleList{
    if (!_leftTitleList) {
        _leftTitleList = @[@"需求类型",@"价格",@"品牌",@"数量",@"信号",@"配备操作人员",@"联系人"];
    }
    return _leftTitleList;
    
}


- (BMKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[BMKMapView alloc] init];
        _mapView.x = 10;
        _mapView.y = 0;
        _mapView.width = Main_Screen_Width - _mapView.x * 2;
        _mapView.height = 360;
        _mapView.showsUserLocation = YES;//显示定位地图层
        _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态为跟随模式
        _mapView.delegate = self;
        _mapView.isSelectedAnnotationViewFront = YES;
        
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
        [_locService startUserLocationService];
        
        
    }
    return _mapView;
}

- (void)setUpView{
    
    self.navigationItem.title = @"求租详情";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"sy_sy" highIcon:@"sy_sy" target:self action:@selector(rightAction)];
    
    
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 44;
    
    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 380)];
    tableFooterView.backgroundColor = [UIColor whiteColor];;
    [tableFooterView addSubview:self.mapView];
    
    _tableView.tableFooterView = tableFooterView;
}

#pragma mark <BMKLocationServiceDelegate>

//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    //    NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"当前位置 %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    self.mapView.centerCoordinate = coor;
}

#pragma mark <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 1:
            return self.leftTitleList.count;
            break;
            
        default:
             return 1;
            break;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    switch (indexPath.section) {
        case 0:
        {
            ZHFindRentMsgDetailsOneCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellOne];
            
            if(!cell){
                cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZHFindRentMsgDetailsOneCell class]) owner:nil options:nil] lastObject];
            }
            
            return cell;
        }
            break;
        case 1:
        {
            return [self tableView:tableView withIndexPath:indexPath];;
        }
        case 2:
        case 3:
        {
            ZHFindRentMsgDetailsThreeCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellOne];
            
            if(!cell){
                cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZHFindRentMsgDetailsThreeCell class]) owner:nil options:nil] lastObject];
            }
            
            return cell;
        }
            
            break;
        default:
            break;
    }
   
    return [[UITableViewCell alloc] init];
}

//第二组cell
- (ZHFindRentMsgDetailsTowCell *)tableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath{
    ZHFindRentMsgDetailsTowCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellTow];
    
    if(!cell){
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZHFindRentMsgDetailsTowCell class]) owner:nil options:nil] lastObject];
    }
    cell.leftL.text = self.leftTitleList[indexPath.row];
    if (indexPath.row == 1) {
        cell.vauleL.textColor = UIColorFromRGB(0xff8650);
    }else{
        cell.vauleL.textColor = [UIColor gb_textColor_4D4D4D];
    }
    return cell;
}

#pragma mark <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 25;
    }
    else if (section == 3){
        return 0.000001;
    }
    else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            case 1:
            return 0.00000001;
            break;
        default:
            return 50;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return 50;
    }else{
        return UITableViewAutomaticDimension;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, [self tableView:tableView heightForHeaderInSection:section])];
    header.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, Main_Screen_Width - 40, header.height)];
    lable.font = SYSTEMFONT(12);
    lable.textColor = [UIColor gb_textColor_999999];
    if (section == 2) {
        lable.text = @"求租详情";
    }else if (section == 3){
        lable.text = @"施工地点";
    }else{
        lable.text = @"";
    }
    [header addSubview:lable];
    
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    ZHFindRentMsgDetailsFooterView *footer = [ZHFindRentMsgDetailsFooterView initWithXib];
    footer.frame = CGRectMake(0, 0, Main_Screen_Width, [self tableView:tableView heightForFooterInSection:section]);
    footer.autoresizingMask = UIViewAutoresizingNone;
    
    if (section == 0) {
        footer.time = @"发布时间：2017-05-19";
    }else{
        footer.time = @"";
    }
    
    if (section == 3) {
        footer.lineH = 0;
    }else{
        footer.lineH = 0.5;
    }
    
    return footer;
}

- (void)rightAction{
    
    
}

@end
